package com.etime.shycourse.controller.ad;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shy.common.BaseMessage;
import com.etime.shy.common.PojoMessage;
import com.etime.shycourse.config.PageMessage;
import com.etime.shycourse.config.StatisticsMessage;
import com.etime.shycourse.dto.AdLogDto;
import com.etime.shycourse.pojo.AdLog;
import com.etime.shycourse.pojo.Course;
import com.etime.shycourse.services.ad.interfaces.AdLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeParseException;
import java.util.Date;
import java.util.List;

/**
 * 广告投放记录控制层
 */
@Api(tags = "后台-广告投放记录管理数据接口")
@RestController
@RequestMapping("/sys/adlog")
public class AdLogController {
    @Autowired
    private AdLogService adLogService;

    @ApiOperation("获取指定广告位中所有广告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "placeid",value = "广告位的id"),
            @ApiImplicitParam(name = "pageNum",value = "当前第几页"),
            @ApiImplicitParam(name = "pageSize",value = "每页多少数据")
    })
    @GetMapping("/page")
    public PageMessage<AdLogDto> selectAdLogPage(AdLogDto adLogDto, Long pageNum, Long pageSize){
        PageMessage<AdLogDto> message = new PageMessage<>();
        Page<AdLogDto> adLogPage = adLogService.selectAdLogPage(adLogDto, pageNum, pageSize);
        if (adLogPage != null && adLogPage.getRecords().size() > 0){
            message.setCode(1);
            message.setPagedata(adLogPage);
            message.setMsg("数据查询成功");
        }else {
            message.setCode(0);
            message.setPagedata(adLogPage);
            message.setMsg("未查询到数据");
        }
        return message;
    }

    @ApiOperation("根据广告位限定的课程类型分页查询匹配的课程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "placeCourseTypeId",value = "广告位的限定课程类型id"),
            @ApiImplicitParam(name = "coursetitle",value = "查询条件课程标题"),
            @ApiImplicitParam(name = "pageNum",value = "当前第几页"),
            @ApiImplicitParam(name = "pageSize",value = "每页多少数据")
    })
    @GetMapping("/get/by/{placeCourseTypeId}")
    public PageMessage<Course> selectCourseByPlaceCourseTypeId(
            @PathVariable("placeCourseTypeId") Long placeCourseTypeId,
            String coursetitle,
            Long pageNum,
            Long pageSize){
        PageMessage<Course> message = new PageMessage<>();
        Page<Course> coursePage = adLogService
                .selectCourseByPlaceCourseTypeId(placeCourseTypeId,coursetitle, pageNum, pageSize);
        if (coursePage!=null && coursePage.getRecords().size()>0){
            message.setCode(1);
            message.setMsg("数据查询成功");
            message.setPagedata(coursePage);
        }else {
            message.setCode(0);
            message.setMsg("未查询到数据");
        }
        return message;
    }
    @ApiOperation("根据广告位统计已投放和等待投放的广告在近一个月内的每天的个数,返回map")
    @ApiImplicitParam(name = "placeid",value = "广告位id")
    @GetMapping("/get/by/day/{placeid}")
    public List<StatisticsMessage> statisticsAdLogByPlaceId(@PathVariable("placeid") Long placeId){
        return adLogService.statisticsAdLogByPlaceId(placeId);
    }
    @ApiOperation("添加一条广告投放记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "placeid",value = "广告位id"),
            @ApiImplicitParam(name = "adimg",value = "广告图片地址"),
            @ApiImplicitParam(name = "adcourseid",value = "广告课程id"),
            @ApiImplicitParam(name = "adlink",value = "广告链接"),
            @ApiImplicitParam(name = "adstart",value = "广告投放开始日期"),
            @ApiImplicitParam(name = "adend",value = "广告投放结束日期"),
            @ApiImplicitParam(name = "adprice",value = "广告投放每天价格"),
            @ApiImplicitParam(name = "adsumprice",value = "广告投放总金额"),
            @ApiImplicitParam(name = "adsort",value = "广告排序"),
            @ApiImplicitParam(name = "adstate",value = "广告状态，1-正常，2-未上架，3-已下架，默认为1")
    })
    @PostMapping("/add")
    public BaseMessage addAdLog(AdLog adLog){
        BaseMessage message = new BaseMessage();
        Integer rtn = adLogService.addAdLog(adLog);
        if (rtn > 0){
            message.setCode(1);
            message.setMsg("数据添加成功");
        }else {
            message.setCode(0);
            message.setMsg("数据添加失败");
        }
        return message;
    }
    @ApiOperation("根据广告id查询广告记录")
    @ApiImplicitParam(name = "adid",value = "广告记录的id")
    @GetMapping("/get/{adid}")
    public PojoMessage<AdLog> selectAdLog(@PathVariable("adid") Long adid){
        PojoMessage<AdLog> message = new PojoMessage<>();
        AdLog adLog = adLogService.selectById(adid);
        if (adLog != null){
            message.setCode(1);
            message.setMsg("数据查询成功");
            message.setData(adLog);
        }else {
            message.setCode(0);
            message.setMsg("未查询到数据");
        }
        return message;
    }

    @ApiOperation("修改一条广告投放记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "placeid",value = "广告位id"),
            @ApiImplicitParam(name = "adimg",value = "广告图片地址"),
            @ApiImplicitParam(name = "adcourseid",value = "广告课程id"),
            @ApiImplicitParam(name = "adlink",value = "广告链接"),
            @ApiImplicitParam(name = "adstart",value = "广告投放开始日期"),
            @ApiImplicitParam(name = "adend",value = "广告投放结束日期"),
            @ApiImplicitParam(name = "adprice",value = "广告投放每天价格"),
            @ApiImplicitParam(name = "adsumprice",value = "广告投放总金额"),
            @ApiImplicitParam(name = "adsort",value = "广告排序"),
            @ApiImplicitParam(name = "adstate",value = "广告状态，1-正常，2-未上架，3-已下架，默认为1")
    })
    @PostMapping("/update")
    public BaseMessage updateAdLog(AdLog adLog){
        BaseMessage message = new BaseMessage();
        Integer rtn = adLogService.updateAdLog(adLog);
        if (rtn > 0){
            message.setCode(1);
            message.setMsg("数据修改成功");
        }else {
            message.setCode(0);
            message.setMsg("数据修改失败");
        }
        return message;
    }

}
